//
//  BuyAiCountItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/5/8.
//

import SwiftUI

struct BuyAiCountItemView: View {
    @State var bean: BuyCountBean
    let isSelected: Bool
    var body: some View {
        HStack {
            VStack{
                Text(bean.count?.stringValue ?? "0")
                    .font(.system(size: 16))
                    .foregroundStyle(themeManager.themeColor)
                Text("+\(bean.giftCount?.stringValue ?? "0")")
                    .font(.system(size: 10))
                    .foregroundStyle(themeManager.themeColor)
            }.frame(width: 50,height: 50)
                .background(themeManager.themeColor.opacity(0.2))
                .clipShape(Circle())
            VStack(alignment:.leading) {
                Text(bean.title ?? "")
                    .font(.system(size: 14))
                    .foregroundStyle(.bgNormalText)
                Text(bean.subtitle ?? "")
                    .font(.system(size: 12))
                    .foregroundStyle(.bgNormalText3)
            }
            Spacer()
            HStack(alignment:.bottom,spacing: 0){
                Text("¥")
                    .font(.system(size: 14))
                    .foregroundStyle(themeManager.themeColor)
                    .padding(.vertical,4)
                Text(getPice())
                    .font(.system(size: 28))
                    .foregroundStyle(themeManager.themeColor)
                Text(".")
                    .font(.system(size: 14))
                    .foregroundStyle(themeManager.themeColor)
                    .padding(.vertical,3)
                Text(getPice2())
                    .font(.system(size: 14))
                    .foregroundStyle(themeManager.themeColor)
                    .padding(.vertical,3)
            }
        }.frame(maxWidth: .infinity, alignment: .leading)
            .padding(.horizontal,10)
            .padding(.vertical, 20)
            .background(isSelected ? themeManager.themeColor.opacity(0.15) : .bgItem)
            .clipShape(RoundedRectangle(cornerRadius: 8))
            .padding(.horizontal,10)
    }
    
    func getPice() -> String {
        let parts = String(bean.price ?? 0.0).components(separatedBy: ".")
        if parts.count == 2 {
            let integerPart = parts[0]  // "3"
            return integerPart
        }
        return bean.price?.stringValue ?? "0"

    }
    
    func getPice2() -> String {
        let parts = String(bean.price ?? 0.0).components(separatedBy: ".")
        if parts.count == 2 {
            let fractionalPart = parts[1] // "14"
            return fractionalPart
        }
        return bean.price?.stringValue ?? "0"

    }
}

#Preview {
    let bean = BuyCountBean()
    bean.count = 100
    bean.giftCount = 10
    bean.title = "我是标题"
    bean.subtitle = "我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题我是副标题"
    bean.price = 92.3
    return BuyAiCountItemView(bean: bean,isSelected: true)
}
